package com.example.budget3.months;

import java.util.Calendar;
import java.util.List;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;

public class MonthPagerAdapter extends FragmentStatePagerAdapter {
	
	private int count;
	private Calendar latest;
	private FragmentManager fm;
	
    public static final String ARG_MONTH_NUMBER = "month_number";
    public static final String ARG_LATEST = "latest";

	public MonthPagerAdapter(FragmentManager fm, int count, Calendar latest) {
        super(fm);
        this.fm = fm;
        this.count = count;
        this.latest = latest;
        
    }
	
    public Fragment getItem(int position) {
    	ListFragment lineFragment = new ListFragment();
		Bundle args = new Bundle();
        args.putInt(ARG_MONTH_NUMBER, position);
        args.putLong(ARG_LATEST, latest.getTimeInMillis());
        lineFragment.setArguments(args);
        return lineFragment;
	}

	@Override
	public int getCount() {
		return count;
	}
	
	public void setCount(int count) {
		this.count = count;
	}
	
	@Override
	public void notifyDataSetChanged() {
		super.notifyDataSetChanged();
		if(fm != null){
			List<Fragment> fragments = fm.getFragments();
			if(fragments != null){
				for(Fragment frag : fragments) {
					if(frag instanceof ListFragment){
						((ListFragment)frag).checkForChanges();
					}
				}
			}
		}
	}
}
